package id.ac.amikom.amikomsocial;

import java.io.InputStream;

import id.ac.amikom.libs.AdemServiceHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class AdemUserActivity extends Activity {

	private String id;
	private TextView usrName, usrAlias, usrReg, usrLoc, usrShout;
	private ImageView usrImg;
	

	private class LoadData extends AsyncTask<Void, Void, Boolean> {

		private ProgressDialog dialog = new ProgressDialog(
				AdemUserActivity.this);
		private JSONArray jsonData;
		private Bitmap foto;

		protected void onPreExecute() {
			dialog.setMessage("Loading..");
			dialog.show();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			try {
				JSONObject json = jsonData.getJSONObject(0);
				
				if (!json.getString("foto").equals("")) {					
					usrImg.setImageBitmap(foto);
				}else{
					usrImg.setImageDrawable(getResources().getDrawable(R.drawable.none));
				}

				usrName.setText(json.getString("fullname")+" ("+ id +")");				
				
				String sts = "";
				if(json.getString("status").equals("1")) sts = "Student";
				else if(json.getString("status").equals("2")) sts = "Lecturer";
				else sts = "Alumnus";
				
				usrAlias.setText(json.getString("alias")+" ("+ sts +")");
				usrReg.setText("Reg.Date "+json.getString("logdate"));
				usrShout.setText(json.getString("shout")+" Shout");
				usrLoc.setText(json.getString("device")+" from "+json.getString("location"));
				
				
			} catch (JSONException e) {

				e.printStackTrace();
			}
			
			dialog.dismiss();
		}

		@Override
		protected Boolean doInBackground(Void... arg0) {

			AdemServiceHelper srv = new AdemServiceHelper(AdemUserActivity.this);
			jsonData = srv.getUser(id);		
			
			try {
				
				JSONObject json = jsonData.getJSONObject(0);
				
	            InputStream in = new java.net.URL(json.getString("foto")).openStream();
	            foto = BitmapFactory.decodeStream(in);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }

			return true;
		}

	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.adem_user);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.adem_title);

		Bundle extras = getIntent().getExtras();
		id = extras.getString("nid");

		usrImg = (ImageView) findViewById(R.id.usrImg);
		usrName = (TextView) findViewById(R.id.usrName);
		usrAlias = (TextView) findViewById(R.id.usrAlias);
		usrReg = (TextView) findViewById(R.id.usrReg);
		usrLoc = (TextView) findViewById(R.id.usrLoc);
		usrShout = (TextView) findViewById(R.id.usrShout);

		new LoadData().execute();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {

			Intent i = new Intent(AdemUserActivity.this, AdemTabActivity.class);
			i.putExtra("url", "");
			startActivity(i);
			finish();

			return false;

		}
		return super.onKeyDown(keyCode, event);
	}

}
